<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入服装名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="条形码" prop="barcode">
        <el-input
          v-model="queryParams.barcode"
          placeholder="请输入条形码"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="类别" prop="cid">
        <el-select v-model="queryParams.cid" placeholder="请选择类别">
            <el-option
              v-for="item in categoryList"
              :key="item.id"
              :label="item.name"
              :value="item.id"
              @keyup.enter.native="handleQuery"
              >
            </el-option>
          </el-select>
      </el-form-item>
      <el-form-item label="材料" prop="mid">
        <el-select v-model="queryParams.mid" placeholder="请选择材料">
            <el-option
              v-for="item in materialList"
              :key="item.id"
              :label="item.name"
              :value="item.id"
              @keyup.enter.native="handleQuery"
              >
            </el-option>
          </el-select>
      </el-form-item>
      <el-form-item label="颜色" prop="coid">
        <el-select v-model="queryParams.coid" placeholder="请选择已有的颜色">
            <el-option
              v-for="item in colorList"
              :key="item.id"
              :label="item.name"
              :value="item.id"
              @keyup.enter.native="handleQuery"
              >
            </el-option>
          </el-select>
      </el-form-item>
      <el-form-item label="尺码" prop="size">
        <el-select v-model="queryParams.size" placeholder="请选择尺码">
            <el-option
              v-for="item in sizeList"
              :key="item.id"
              :label="item.name"
              :value="item.id"
               @keyup.enter.native="handleQuery"
              >
            </el-option>
          </el-select>
      </el-form-item>
      <el-form-item label="类型" prop="type">
        <el-select v-model="queryParams.type" placeholder="请选择类型">
           <el-option
              v-for="item in typeList"
              :key="item.id"
              :label="item.name"
              :value="item.id"
              @keyup.enter.native="handleQuery"
              >
            </el-option>
          </el-select>
      </el-form-item>
      <el-form-item label="售价" prop="salePrice">
        <el-input
          v-model="queryParams.salePrice"
          placeholder="请输入售价"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="进价" prop="inPrice">
        <el-input
          v-model="queryParams.inPrice"
          placeholder="请输入进价"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['pos:goods:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['pos:goods:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['pos:goods:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['pos:goods:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="goodsList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label=" " align="center" prop="id" />
      <el-table-column label="名称" align="center" prop="name" />
      <el-table-column label="条形码" align="center" prop="barcode" />
      <el-table-column label="类别" align="center" prop="cid" 
       :formatter="((row,column)=>{return gDatasFormatter(row,column,categoryList)})"/>
      <el-table-column label="材料" align="center" prop="mid" 
       :formatter="((row,column)=>{return gDatasFormatter(row,column,materialList)})"/>
      <el-table-column label="颜色" align="center" prop="coid" 
        :formatter="((row,column)=>{return gDatasFormatter(row,column,colorList)})"/>
      <el-table-column label="尺码" align="center" prop="size"
      :formatter="((row,column)=>{return gDatasFormatter(row,column,sizeList)})" />
      <el-table-column label="类型" align="center" prop="type"
      :formatter="((row,column)=>{return gDatasFormatter(row,column,typeList)})" />
       <el-table-column label="库存" align="center" prop="inventory" />
      <el-table-column label="售价" align="center" prop="salePrice" />
      <el-table-column label="进价" align="center" prop="inPrice" />
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['pos:goods:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['pos:goods:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改goods对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入名称" />
        </el-form-item>
        <el-form-item label="条形码" prop="barcode">
          <el-input v-model="form.barcode" placeholder="请输入条形码" />
        </el-form-item>
        <el-form-item label="类别" prop="cid">
          <el-select v-model="form.cid" placeholder="请选择类别">
            <el-option
              v-for="item in categoryList"
              :key="item.id"
              :label="item.name"
              :value="item.id"
              >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="材料" prop="mid">
          <el-select v-model="form.mid" placeholder="请选择材料">
            <el-option
              v-for="item in materialList"
              :key="item.id"
              :label="item.name"
              :value="item.id"
              >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="颜色" prop="coid">
           <el-select v-model="form.coid" placeholder="请选择已有的颜色">
            <el-option
              v-for="item in colorList"
              :key="item.id"
              :label="item.name"
              :value="item.id"
              >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="尺码" prop="size">
          <el-select v-model="form.size" placeholder="请选择尺码">
            <el-option
              v-for="item in sizeList"
              :key="item.id"
              :label="item.name"
              :value="item.id"
              >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="类型" prop="type">
          <el-select v-model="form.type" placeholder="请选择类型">
           <el-option
              v-for="item in typeList"
              :key="item.id"
              :label="item.name"
              :value="item.id"
              >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="售价" prop="salePrice">
          <el-input v-model="form.salePrice" placeholder="请输入售价" />
        </el-form-item>
        <el-form-item label="进价" prop="inPrice">
          <el-input v-model="form.inPrice" placeholder="请输入进价" />
        </el-form-item>
        <el-form-item label="状态" prop="state">
          <el-input v-model="form.state" placeholder="请输入状态" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listGoods, getGoods, delGoods, addGoods, updateGoods, exportGoods } from "@/api/system/goods";
import {listColor,getColor} from "@/api/system/color";
import {listCategory} from "@/api/system/category";
import {listMaterial} from "@/api/system/material";

export default {
  name: "Goods",
  components: {
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 子表选中数据
      checkedOrderItem: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // goods表格数据
      goodsList: [],
      // item表格数据
      orderItemList: [],
      // 弹出层标题
      title: "商品",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: null,
        barcode: null,
        cid: null,
        mid: null,
        coid: null,
        size: null,
        type: null,
        salePrice: null,
        inPrice: null,
        state: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      },
      colorList: [],
      categoryList:[],
      materialList:[],
      colorName:null,
      typeList:[
        {
          id:1,
          name:"上架"  
        },
        {
          id:2,
          name:"下架"
        }
      ],
      sizeList:[
        {
          id:1,
          name:"S" 
        },
        {
          id:2,
          name:"M" 
        },
        {
          id:3,
          name:"L" 
        },
        {
          id:4,
          name:"XL" 
        }
      ]
    };
  },
  created() {
    this.getList();
    this.getColors();
    this.getCategory();
    this.getMaterial()
  },
  methods: {
    getColors(){
      listColor(this.queryParams).then(response => {
        this.colorList = response.rows;
        this.total = response.total;
        this.loading = false;
        console.log(this.colorList)
      });
      
    },
    getCategory(){
      listCategory(this.queryParams).then(response => {
        this.categoryList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    getMaterial(){
      listMaterial(this.queryParams).then(response => {
        this.materialList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 查询goods列表 */
    getList() {
      this.loading = true;
      listGoods(this.queryParams).then(response => {
        this.goodsList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
      
    },
    gDatasFormatter(row,column,dicData){
      let results = row[column.property]; // 默认原真实值
      if(dicData && dicData.length>0){
          for(let i=0;i<dicData.length;i++){
              let item = dicData[i];
              if(row[column.property] === item.id){
                  results = item.name;
                  break;
              }
          }
    }
    return results;
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        name: null,
        barcode: null,
        cid: null,
        mid: null,
        coid: null,
        size: null,
        type: null,
        salePrice: null,
        inPrice: null,
        createTime: null,
        state: null,
        remark: null
      };
      this.orderItemList = [];
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加goods";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getGoods(id).then(response => {
        this.form = response.data;
        this.orderItemList = response.data.orderItemList;
        this.open = true;
        this.title = "修改goods";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.form.orderItemList = this.orderItemList;
          if (this.form.id != null) {
            updateGoods(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addGoods(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除goods编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delGoods(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
    },
	/** item序号 */
    rowOrderItemIndex({ row, rowIndex }) {
      row.index = rowIndex + 1;
    },
    /** item添加按钮操作 */
    handleAddOrderItem() {
      let obj = {};
      obj.oid = "";
      obj.number = "";
      obj.salePrice = "";
      obj.subTotal = "";
      this.orderItemList.push(obj);
    },
    /** item删除按钮操作 */
    handleDeleteOrderItem() {
      if (this.checkedOrderItem.length == 0) {
        this.$alert("请先选择要删除的item数据", "提示", { confirmButtonText: "确定", });
      } else {
        this.orderItemList.splice(this.checkedOrderItem[0].index - 1, 1);
      }
    },
    /** 单选框选中数据 */
    handleOrderItemSelectionChange(selection) {
      if (selection.length > 1) {
        this.$refs.orderItem.clearSelection();
        this.$refs.orderItem.toggleRowSelection(selection.pop());
      } else {
        this.checkedOrderItem = selection;
      }
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有goods数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportGoods(queryParams);
        }).then(response => {
          this.download(response.msg);
        })
    }
  }
};
</script>
